

///////////
// Setup //
///////////

clear

set more off

clear mata
clear matrix

set maxvar 15000
set matsize 5000

log close _all
log using "1_Data.smcl", replace


/////////////////
// Survey Data //
/////////////////

import delimited "Survey_Data.csv", clear


///////////////////////
// Admin Data Update //
///////////////////////

// Dates of First and Last Task
///////////////////////////////

merge 1:1 respondentid using "Admin_Data_Tasks_Timestamps.dta", nogen


//////////////////////////////////
// Admin Data Update Revision 1 //
//////////////////////////////////

// Task Behaviour and Responder Characteristics
///////////////////////////////////////////////

merge 1:1 respondentid using "Admin_Data_Task_Behaviour.dta", nogen

rename gender gender_admin
rename religion religion_admin
rename NumberCheckInAndChat numberCheckInAndChat

// LAD IDs
//////////

sort lsoa
merge n:1 lsoa using "Correspondence_LSOA_LAD.dta", nogen

sort respondentid


/////////////////////////////
// Survey Data Preparation //
/////////////////////////////

// Drop Variables
/////////////////

drop collectorid

drop ipaddress

drop emailaddress

drop firstname
drop lastname

drop customdata1

drop beforewebeginthesurveywen
drop v11

drop ifyespleaseprovidecontactdetails
drop v132
drop v133
drop v134
drop v135
drop v136
drop v137
drop v138
drop v139
drop v140

// Recode Variables
///////////////////

// Outcomes

generate satisfaction_life = .
replace satisfaction_life = 0 if(thenexttwoofquestionsaskaboutyou == "0 'not at all'")
replace satisfaction_life = 1 if(thenexttwoofquestionsaskaboutyou == "1")
replace satisfaction_life = 2 if(thenexttwoofquestionsaskaboutyou == "2")
replace satisfaction_life = 3 if(thenexttwoofquestionsaskaboutyou == "3")
replace satisfaction_life = 4 if(thenexttwoofquestionsaskaboutyou == "4")
replace satisfaction_life = 5 if(thenexttwoofquestionsaskaboutyou == "5")
replace satisfaction_life = 6 if(thenexttwoofquestionsaskaboutyou == "6")
replace satisfaction_life = 7 if(thenexttwoofquestionsaskaboutyou == "7")
replace satisfaction_life = 8 if(thenexttwoofquestionsaskaboutyou == "8")
replace satisfaction_life = 9 if(thenexttwoofquestionsaskaboutyou == "9")
replace satisfaction_life = 10 if(thenexttwoofquestionsaskaboutyou == "10 'completely'")

generate worthwhileness_life = .
replace worthwhileness_life = 0 if(v100 == "0 'not at all'")
replace worthwhileness_life = 1 if(v100 == "1")
replace worthwhileness_life = 2 if(v100 == "2")
replace worthwhileness_life = 3 if(v100 == "3")
replace worthwhileness_life = 4 if(v100 == "4")
replace worthwhileness_life = 5 if(v100 == "5")
replace worthwhileness_life = 6 if(v100 == "6")
replace worthwhileness_life = 7 if(v100 == "7")
replace worthwhileness_life = 8 if(v100 == "8")
replace worthwhileness_life = 9 if(v100 == "9")
replace worthwhileness_life = 10 if(v100 == "10 'completely'")

generate belongingness = .
replace belongingness = 1 if(howstronglydoyoufeelyoubelongtoy == "Very strongly" | howstronglydoyoufeelyoubelongtoy == "Fairly strongly")
replace belongingness = 0 if(howstronglydoyoufeelyoubelongtoy == "Not very strongly" | howstronglydoyoufeelyoubelongtoy == "Not at all strongly")

generate connectedness = .
replace connectedness = 1 if(doyoufeelmoreorlessconnectedtoyo == "More connected")
replace connectedness = 0 if(doyoufeelmoreorlessconnectedtoyo == "About the same" | doyoufeelmoreorlessconnectedtoyo == "Less connected")

generate prosociality = .
replace prosociality = 1 if(doyouthinkpeoplearedoingthingsto == "More")
replace prosociality = 0 if(doyouthinkpeoplearedoingthingsto == "About the same" | doyouthinkpeoplearedoingthingsto == "Less" | doyouthinkpeoplearedoingthingsto == "Donât know")
replace prosociality = 0 if(strpos(doyouthinkpeoplearedoingthingsto, "know"))

// Treatment Variables

// // Given Task and Volunteered

generate volunteered = .
replace volunteered = 1 if(haveyoubeengivenataskyet == "Yes")
replace volunteered = 0 if(haveyoubeengivenataskyet == "No")

generate reason_not_volunteered = .

// // No Task Given Yet Because Forgot to Switch App 'On Duty'
replace reason_not_volunteered = 1 if(ifyouhavenotyetbeenalertedoffere == "I haven't yet been given a task to do because I forgot to switch my app 'on-duty'")

// // No Task Given Yet Despite Switching App 'On Duty'
replace reason_not_volunteered = 2 if(ifyouhavenotyetbeenalertedoffere == "I haven't yet been given a task to do despite switching the app 'on-duty'")
replace reason_not_volunteered = 2 if(strpos(ifyouhavenotyetbeenalertedoffere, "despite"))

// // Given Task But Unable to Accept
replace reason_not_volunteered = 3 if(ifyouhavenotyetbeenalertedoffere == "I was given a task but unable to accept (e.g. was busy at the time, task was outside my area, etc)")

// // Other Reason
replace reason_not_volunteered = 4 if(ifyouhavenotyetbeenalertedoffere == "Other (please specify)")

// // Survey Data Treatment Dummy (Given Task and Volunteered Versus No Task Given Yet Despite Switching App 'On Duty')
generate treatment_s = .
replace treatment_s = 1 if(volunteered == 1)
replace treatment_s = 0 if(volunteered == 0 & reason_not_volunteered == 2)

// // Number of Tasks

generate number_tasks_s = .
replace number_tasks_s = 1 if(howmanytaskshaveyoucompletedtoda == "1")
replace number_tasks_s = 2 if(howmanytaskshaveyoucompletedtoda == "2")
replace number_tasks_s = 3 if(howmanytaskshaveyoucompletedtoda == "3")
replace number_tasks_s = 4 if(howmanytaskshaveyoucompletedtoda == "4")
replace number_tasks_s = 5 if(howmanytaskshaveyoucompletedtoda == "5")
replace number_tasks_s = 6 if(howmanytaskshaveyoucompletedtoda == "6")
replace number_tasks_s = 7 if(howmanytaskshaveyoucompletedtoda == "7")
replace number_tasks_s = 8 if(howmanytaskshaveyoucompletedtoda == "8")
replace number_tasks_s = 9 if(howmanytaskshaveyoucompletedtoda == "9")
replace number_tasks_s = 10 if(howmanytaskshaveyoucompletedtoda == "10")
replace number_tasks_s = 11 if(howmanytaskshaveyoucompletedtoda == "more than 10")

// Covariates

generate age = .
replace age = 1 if(howoldareyou == "Under 16")
replace age = 2 if(v106 == "16 to 24")
replace age = 3 if(v107 == "25 to 34")
replace age = 4 if(v108 == "35 to 44")
replace age = 5 if(v109 == "45 to 54")
replace age = 6 if(v110 == "55 to 64")
replace age = 7 if(v111 == "65 to 74")
replace age = 8 if(v112 == "75 to 84")
replace age = 9 if(v113 == "85 or over")
replace age = 10 if(v114 == "Prefer not to say")

generate age2 = .
replace age2 = 1 if(howoldareyou == "Under 16" | v106 == "16 to 24")
replace age2 = 2 if(v107 == "25 to 34")
replace age2 = 3 if(v108 == "35 to 44")
replace age2 = 4 if(v109 == "45 to 54")
replace age2 = 5 if(v110 == "55 to 64")
replace age2 = 6 if(v111 == "65 to 74" | v112 == "75 to 84" | v113 == "85 or over")
replace age2 = 7 if(v114 == "Prefer not to say")

generate gender = .
replace gender = 1 if(pleasespecifyyourgender == "Male")
replace gender = 2 if(v116 == "Female")
replace gender = 3 if(v117 == "Other")
replace gender = 4 if(v118 == "Prefer not to say")

generate sexuality = .
replace sexuality = 1 if(whichofthefollowingbestdescribes == "Heterosexual or straight")
replace sexuality = 2 if(v120 == "Gay or lesbian")
replace sexuality = 3 if(v121 == "Bisexual")
replace sexuality = 4 if(v122 == "Other")
replace sexuality = 5 if(v123 == "I would prefer not to say")

generate ethnicity = .
replace ethnicity = 1 if(whatisyourethnicgroup == "Asian / Asian British: Bangladeshi")
replace ethnicity = 2 if(whatisyourethnicgroup == "Asian / Asian British: Chinese")
replace ethnicity = 3 if(whatisyourethnicgroup == "Asian / Asian British: Indian")
replace ethnicity = 4 if(whatisyourethnicgroup == "Asian / Asian British:Pakistani")
replace ethnicity = 5 if(whatisyourethnicgroup == "Asian / Asian British: Any other Asian background")
replace ethnicity = 6 if(whatisyourethnicgroup == "Black / African / Caribbean / Black British: African")
replace ethnicity = 7 if(whatisyourethnicgroup == "Black / African / Caribbean / Black British: Caribbean")
replace ethnicity = 8 if(whatisyourethnicgroup == "Black / African / Caribbean / Black British: Any other Black / African / Caribbean")
replace ethnicity = 9 if(whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Asian")
replace ethnicity = 10 if(whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Black African")
replace ethnicity = 11 if(whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Black Caribbean")
replace ethnicity = 12 if(whatisyourethnicgroup == "Mixed / multiple ethnic groups: Any other Mixed / multiple ethnic background")
replace ethnicity = 13 if(whatisyourethnicgroup == "White: British / English / Northern Irish / Scottish / Welsh")
replace ethnicity = 14 if(whatisyourethnicgroup == "White: Gypsy or Irish Traveller")
replace ethnicity = 15 if(whatisyourethnicgroup == "White: Irish")
replace ethnicity = 16 if(whatisyourethnicgroup == "White: Any other White background")
replace ethnicity = 17 if(whatisyourethnicgroup == "Other ethnic group: Arab")
replace ethnicity = 18 if(whatisyourethnicgroup == "Other ethnic group: Any other ethnic group")

generate ethnicity2 = .
replace ethnicity2 = 1 if(whatisyourethnicgroup == "Asian / Asian British: Bangladeshi" | whatisyourethnicgroup == "Asian / Asian British: Chinese" | whatisyourethnicgroup == "Asian / Asian British: Indian" | whatisyourethnicgroup == "Asian / Asian British:Pakistani" | whatisyourethnicgroup == "Asian / Asian British: Any other Asian background")
replace ethnicity2 = 2 if(whatisyourethnicgroup == "Black / African / Caribbean / Black British: African" | whatisyourethnicgroup == "Black / African / Caribbean / Black British: Caribbean" | whatisyourethnicgroup == "Black / African / Caribbean / Black British: Any other Black / African / Caribbean")
replace ethnicity2 = 3 if(whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Asian" | whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Black African" | whatisyourethnicgroup == "Mixed / multiple ethnic groups: White and Black Caribbean" | whatisyourethnicgroup == "Mixed / multiple ethnic groups: Any other Mixed / multiple ethnic background")
replace ethnicity2 = 4 if(whatisyourethnicgroup == "White: British / English / Northern Irish / Scottish / Welsh" | whatisyourethnicgroup == "White: Gypsy or Irish Traveller" | whatisyourethnicgroup == "White: Irish" | whatisyourethnicgroup == "White: Any other White background")
replace ethnicity2 = 5 if(whatisyourethnicgroup == "Other ethnic group: Arab" | whatisyourethnicgroup == "Other ethnic group: Any other ethnic group")

generate condition = .
replace condition = 1 if(doyouhaveanylongtermphysicalorme == "No")
replace condition = 2 if(doyouhaveanylongtermphysicalorme == "Yes")
replace condition = 3 if(doyouhaveanylongtermphysicalorme == "Don't know / can't say")
replace condition = 3 if(strpos(doyouhaveanylongtermphysicalorme, "know"))
replace condition = 4 if(doyouhaveanylongtermphysicalorme == "I would prefer not to say")

generate isolating = .
replace isolating = 1 if(haveyoubeenadvisedtoshieldorself == "No")
replace isolating = 2 if(haveyoubeenadvisedtoshieldorself == "Yes")
replace isolating = 3 if(haveyoubeenadvisedtoshieldorself == "Don't know")

generate employment = .
replace employment = 1 if(whichofthesebestdescribeswhatyou == "Full-time paid work (30 hours or more each week)")
replace employment = 2 if(whichofthesebestdescribeswhatyou == "Part-time paid work (under 30 hours each week)")
replace employment = 3 if(whichofthesebestdescribeswhatyou == "Furloughed from work (e.g. due to COVID-19)")
replace employment = 4 if(whichofthesebestdescribeswhatyou == "Full-time education at school, college or university")
replace employment = 5 if(whichofthesebestdescribeswhatyou == "Unemployed")
replace employment = 6 if(whichofthesebestdescribeswhatyou == "Permanently sick or disabled")
replace employment = 7 if(whichofthesebestdescribeswhatyou == "Fully retired from work")
replace employment = 8 if(whichofthesebestdescribeswhatyou == "Looking after the family or home")
replace employment = 9 if(whichofthesebestdescribeswhatyou == "Doing something else")

generate religion = .
replace religion = 1 if(whichifanyofthefollowingbestdesc == "No religion")
replace religion = 2 if(whichifanyofthefollowingbestdesc == "Buddhist")
replace religion = 3 if(whichifanyofthefollowingbestdesc == "Christian (including Church of England, Catholic, Protestant, and other Christian denominations)")
replace religion = 4 if(whichifanyofthefollowingbestdesc == "Hindu")
replace religion = 5 if(whichifanyofthefollowingbestdesc == "Jewish")
replace religion = 6 if(whichifanyofthefollowingbestdesc == "Muslim")
replace religion = 7 if(whichifanyofthefollowingbestdesc == "Sikh")
replace religion = 8 if(whichifanyofthefollowingbestdesc == "Other")
replace religion = 9 if(whichifanyofthefollowingbestdesc == "I would prefer not to say")

generate volunteered_before = .
replace volunteered_before = 1 if(haveyouevervolunteeredbeforeeggi == "Yes, I have")
replace volunteered_before = 0 if(haveyouevervolunteeredbeforeeggi == "No, I have not")
replace volunteered_before = 0 if(haveyouevervolunteeredbeforeeggi == "Don't know/can't recall")
replace volunteered_before = 0 if(strpos(haveyouevervolunteeredbeforeeggi, "recall"))

generate volunteered_elsewhere = .
replace volunteered_elsewhere = 1 if(otherthanparticipationinthenhsvo == "No, I am just helping out with NHS VR")
replace volunteered_elsewhere = 2 if(otherthanparticipationinthenhsvo == "Yes, I am helping out with one other group")
replace volunteered_elsewhere = 3 if(otherthanparticipationinthenhsvo == "Yes, I am helping out with more than one other group")

generate volunteered_elsewhere_where = .
replace volunteered_elsewhere_where = 0 if(volunteered_elsewhere == 1)
replace volunteered_elsewhere_where = 1 if(whatarethesegroupstickallthatapp == "A local informal neighbourhood group(s) which was/are organised by neighbours (e.g WhatApp groups? other)")
replace volunteered_elsewhere_where = 2 if(v15 == "Organised local/community groups (e.g. mutual aid groups, local voluntary groups, etc)")
replace volunteered_elsewhere_where = 3 if(v16 == "National charities – such as Marie Curie, Macmillan, Alzheimer's Society, etc")
replace volunteered_elsewhere_where = 3 if(strpos(v16, "National charities"))
replace volunteered_elsewhere_where = 4 if(v17 == "Public sector (e.g. other NHS voluntary activities, police, fire, etc)")
replace volunteered_elsewhere_where = 5 if(v18 == "Sports group")
replace volunteered_elsewhere_where = 6 if(v19 == "Faith group")
replace volunteered_elsewhere_where = 7 if(v20 == "Other")

generate region = .
replace region = 1 if(inwhichregiondoyoulive == "East of England")
replace region = 2 if(inwhichregiondoyoulive == "London")
replace region = 3 if(inwhichregiondoyoulive == "Midlands")
replace region = 4 if(inwhichregiondoyoulive == "North East and Yorkshire")
replace region = 5 if(inwhichregiondoyoulive == "North West")
replace region = 6 if(inwhichregiondoyoulive == "South East")
replace region = 7 if(inwhichregiondoyoulive == "South West")

// // Motivations

generate motivations_1 = 0
replace motivations_1 = 1 if(whatmotivatedyoutojointhenhsvolu == "I was responding to a national crisis")
generate motivations_2 = 0
replace motivations_2 = 1 if(v22 == "I was furloughed so I had some time")
generate motivations_3 = 0
replace motivations_3 = 1 if(v23 == "I wanted to support the NHS")
generate motivations_4 = 0
replace motivations_4 = 1 if(v24 == "I wanted to make a difference")
generate motivations_5 = 0
replace motivations_5 = 1 if(v25 == "I wanted to help my local community")
generate motivations_6 = 0
replace motivations_6 = 1 if(v26 == "I have an interest in pursuing a career in healthcare/NHS")
generate motivations_7 = 0
replace motivations_7 = 1 if(v27 == "I wanted to gain or use skills and experience")
generate motivations_8 = 0
replace motivations_8 = 1 if(v28 == "I enjoy helping other people")
generate motivations_9 = 0
replace motivations_9 = 1 if(v29 == "I wanted to meet people/make friends")
generate motivations_10 = 0
replace motivations_10 = 1 if(v30 == "Someone asked me to give help")
generate motivations_11 = 0
replace motivations_11 = 1 if(v31 == "It was connected with the needs of my family/friends")
generate motivations_12 = 0
replace motivations_12 = 1 if(v32 == "I thought it was expected of me")
generate motivations_13 = 0
replace motivations_13 = 1 if(v33 == "I enjoy telling my friends/family about my volunteer work")
generate motivations_14 = 0
replace motivations_14 = 1 if(v34 != "")

* Motivation: Altruism (i.e. Helping in National Crisis)
generate motivations2_1 = 0
replace motivations2_1 = 1 if(motivations_1 == 1 | motivations_3 == 1 | motivations_4 == 1 | motivations_5 == 1)
* Thought It Was Expected
generate motivations2_2 = 0
replace motivations2_2 = 1 if(motivations_12 == 1)
* Enjoy Telling Family, Friends
generate motivations2_3 = 0
replace motivations2_3 = 1 if(motivations_13 == 1)
* Was Asked
generate motivations2_4 = 0
replace motivations2_4 = 1 if(motivations_10 == 1)
* Enjoy Helping People
generate motivations2_5 = 0
replace motivations2_5 = 1 if(motivations_8 == 1)
* Wanted to Meet People
generate motivations2_6 = 0
replace motivations2_6 = 1 if(motivations_9 == 1)
* Gain Skills
generate motivations2_7 = 0
replace motivations2_7 = 1 if(motivations_7 == 1)
* Pursue Career
generate motivations2_8 = 0
replace motivations2_8 = 1 if(motivations_6 == 1)
* Had Some Time
generate motivations2_9 = 0
replace motivations2_9 = 1 if(motivations_2 == 1)
* Other
generate motivations2_10 = 0
replace motivations2_10 = 1 if(motivations_14 == 1)

// // Services

generate services_1 = 0
replace services_1 = 1 if(v40 == "NHS Transport")
generate services_2 = 0
replace services_2 = 1 if(v39 == "NHS Patient Transport")
generate services_3 = 0
replace services_3 = 1 if(whatvolunteerroledidyousignupfor == "Community Response")
generate services_4 = 0
replace services_4 = 1 if(v36 == "Community Response PLUS")
generate services_5 = 0
replace services_5 = 1 if(v37 == "Check In and Chat")
generate services_6 = 0
replace services_6 = 1 if(v38 == "Check In and Chat Plus")

generate services2_1 = 0
replace services2_1 = 1 if(services_1 == 1 | services_2 == 1)
generate services2_2 = 0
replace services2_2 = 1 if(services_3 == 1 | services_4 == 1)
generate services2_3 = 0
replace services2_3 = 1 if(services_5 == 1 | services_6 == 1)

// // Dates

generate temp = substr(startdate, 1, 10)
generate date = date(temp, "MDY")
format date %td
drop temp

generate double start = clock(startdate, "MDY hms")
generate double end = clock(enddate, "MDY hms")
generate fidelity = end - start

generate week_day = dow(date)

generate temp = substr(first_task, 1, 9)
generate firsttask = date(temp, "DMY")
format firsttask %td
drop temp

generate temp = substr(last_task, 1, 9)
generate lasttask = date(temp, "DMY")
format lasttask %td
drop temp

generate diff_date_firsttask = date - firsttask
generate diff_date_lasttask = date - lasttask

generate signup = d(01apr2020)
format signup %td

generate waiting_time = date - signup
replace waiting_time = date - firsttask if(firsttask != . & firsttask <= date)

// Drop Implausible Observations

drop if(volunteered == 1 & reason_not_volunteered == 1)
drop if(volunteered == 1 & reason_not_volunteered == 2)
drop if(volunteered == 1 & reason_not_volunteered == 3)
drop if(volunteered == 1 & reason_not_volunteered == 4)

drop if(number_tasks_s != . & number_tasks_s != 0 & reason_not_volunteered == 1)
drop if(number_tasks_s != . & number_tasks_s != 0 & reason_not_volunteered == 2)
drop if(number_tasks_s != . & number_tasks_s != 0 & reason_not_volunteered == 3)
drop if(number_tasks_s != . & number_tasks_s != 0 & reason_not_volunteered == 4)


///////////////////////
// Admin Data Update //
///////////////////////

// Regional Covid-19 Controls
/////////////////////////////

sort region date

// Patients Admitted to Hospital

merge n:1 region date using "Admin_Data_Covid_Admitted_East_of_England.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_London.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_Midlands.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_North_East_and_Yorkshire.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_North_West.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_South_East.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Admitted_South_West.dta", nogen update

// Patients in Hospital

merge n:1 region date using "Admin_Data_Covid_Hospital_East_of_England.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_London.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_Midlands.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_North_East_and_Yorkshire.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_North_West.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_South_East.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Hospital_South_West.dta", nogen update

// Patients in Mechanical Ventilation Beds

merge n:1 region date using "Admin_Data_Covid_Ventilation_East_of_England.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_London.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_Midlands.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_North_East_and_Yorkshire.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_North_West.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_South_East.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Ventilation_South_West.dta", nogen update

// Cases

merge n:1 region date using "Admin_Data_Covid_Cases_East_of_England.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_London.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_Midlands.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_North_East_and_Yorkshire.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_North_West.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_South_East.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Cases_South_West.dta", nogen update

// Deaths

merge n:1 region date using "Admin_Data_Covid_Deaths_East_of_England.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_London.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_Midlands.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_North_East_and_Yorkshire.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_North_West.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_South_East.dta", nogen update
merge n:1 region date using "Admin_Data_Covid_Deaths_South_West.dta", nogen update


////////////////////////////
// Admin Data Preparation //
////////////////////////////

// Identifiers
//////////////

// Services

generate service_1 = 0
replace service_1 = 1 if(tier == 10 | tier == 13 | tier == 14 | tier == 15 | tier == 22 | tier == 23 | tier == 30 | tier == 31 | tier == 35 | tier == 51 | tier == 55 | tier == 56 | tier == 63 | tier == 64 | tier == 65 | tier == 66 | tier == 68 | tier == 70 | tier == 4 | tier == 7 | tier == 9 | tier == 12 | tier == 19 | tier == 21 | tier == 27 | tier == 29 | tier == 34 | tier == 36 | tier == 41 | tier == 42 | tier == 46 | tier == 47 | tier == 53 | tier == 57 | tier == 58 | tier == 61)

generate service_2 = 0
replace service_2 = 1 if(tier == 16 | tier == 17 | tier == 18 | tier == 19 | tier == 20 | tier == 21 | tier == 22 | tier == 23 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 46 | tier == 48 | tier == 52 | tier == 55 | tier == 57 | tier == 59 | tier == 61 | tier == 62 | tier == 64 | tier == 66 | tier == 69 | tier == 71 | tier == 2 | tier == 5 | tier == 8 | tier == 9 | tier == 11 | tier == 12 | tier == 14 | tier == 15 | tier == 25 | tier == 28 | tier == 29 | tier == 31 | tier == 38 | tier == 41 | tier == 43 | tier == 45 | tier == 50 | tier == 51 | tier == 53 | tier == 54 | tier == 58 | tier == 60 | tier == 63 | tier == 65)

generate service_12 = 0
replace service_12 = 1 if((tier == 10 | tier == 13 | tier == 14 | tier == 15 | tier == 22 | tier == 23 | tier == 30 | tier == 31 | tier == 35 | tier == 51 | tier == 55 | tier == 56 | tier == 63 | tier == 64 | tier == 65 | tier == 66 | tier == 68 | tier == 70 | tier == 4 | tier == 7 | tier == 9 | tier == 12 | tier == 19 | tier == 21 | tier == 27 | tier == 29 | tier == 34 | tier == 36 | tier == 41 | tier == 42 | tier == 46 | tier == 47 | tier == 53 | tier == 57 | tier == 58 | tier == 61) | (tier == 16 | tier == 17 | tier == 18 | tier == 19 | tier == 20 | tier == 21 | tier == 22 | tier == 23 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 46 | tier == 48 | tier == 52 | tier == 55 | tier == 57 | tier == 59 | tier == 61 | tier == 62 | tier == 64 | tier == 66 | tier == 69 | tier == 71 | tier == 2 | tier == 5 | tier == 8 | tier == 9 | tier == 11 | tier == 12 | tier == 14 | tier == 15 | tier == 25 | tier == 28 | tier == 29 | tier == 31 | tier == 38 | tier == 41 | tier == 43 | tier == 45 | tier == 50 | tier == 51 | tier == 53 | tier == 54 | tier == 58 | tier == 60 | tier == 63 | tier == 65))

generate service_3 = 0
replace service_3 = 1 if(tier == 24 | tier == 25 | tier == 26 | tier == 27 | tier == 28 | tier == 29 | tier == 30 | tier == 31 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 40 | tier == 47 | tier == 49 | tier == 50 | tier == 52 | tier == 56 | tier == 58 | tier == 60 | tier == 61 | tier == 62 | tier == 65 | tier == 66 | tier == 1 | tier == 5 | tier == 6 | tier == 7 | tier == 11 | tier == 12 | tier == 13 | tier == 15 | tier == 17 | tier == 20 | tier == 21 | tier == 23 | tier == 39 | tier == 42 | tier == 44 | tier == 45 | tier == 53 | tier == 54 | tier == 57 | tier == 59 | tier == 63 | tier == 64 | tier == 69 | tier == 70)

generate service_4 = 0
replace service_4 = 1 if(tier == 36 | tier == 37 | tier == 38 | tier == 39 | tier == 40 | tier == 41 | tier == 42 | tier == 43 | tier == 44 | tier == 45 | tier == 46 | tier == 47 | tier == 48 | tier == 49 | tier == 50 | tier == 51 | tier == 52 | tier == 53 | tier == 54 | tier == 55 | tier == 56 | tier == 57 | tier == 58 | tier == 59 | tier == 60 | tier == 61 | tier == 62 | tier == 63 | tier == 64 | tier == 65 | tier == 66 | tier == 67 | tier == 68 | tier == 69 | tier == 70 | tier == 71)

// Treatment Variables
//////////////////////

// Between Services

// // Extensive Margin

generate treatment_a = .
replace treatment_a = 1 if(allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)
replace treatment_a = 0 if(allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) // Include timed-out tasks (if any)
tab treatment_a

// // Intensive Margin

generate treatment_a_int = .
replace treatment_a_int = 1 if(numberAcceptedTasks == 1 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // lowest 10%
replace treatment_a_int = 2 if(numberAcceptedTasks >= 2 & numberAcceptedTasks <= 2 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // 10% to 25%
replace treatment_a_int = 3 if(numberAcceptedTasks > 2 & numberAcceptedTasks <= 7 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // 25% to 50%
replace treatment_a_int = 4 if(numberAcceptedTasks > 7 & numberAcceptedTasks <= 16 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // 50% to 75%
replace treatment_a_int = 5 if(numberAcceptedTasks > 16 & numberAcceptedTasks <= 35 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // 75% to 90%
replace treatment_a_int = 6 if(numberAcceptedTasks > 35 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)) // highest 10%
replace treatment_a_int = 0 if(allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) // Include timed-out tasks (if any)

// Within Services

// // Extensive Margin

generate treatment_a_service12 = .
replace treatment_a_service12 = 1 if(allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))
replace treatment_a_service12 = 0 if((allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) & (service_1 == 1 | service_2 == 1)) // Include timed-out tasks (if any)
tab treatment_a_service12

generate treatment_a_service3 = .
replace treatment_a_service3 = 1 if(allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)
replace treatment_a_service3 = 0 if((allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) & service_3 == 1) // Include timed-out tasks (if any)
tab treatment_a_service3

// // Intensive Margin

generate treatment_a_serv12_int = .
replace treatment_a_serv12_int = 1 if(numberAcceptedTasks == 1 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // lowest 10%
replace treatment_a_serv12_int = 2 if(numberAcceptedTasks >= 2 & numberAcceptedTasks <= 4 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // 10% to 25%
replace treatment_a_serv12_int = 3 if(numberAcceptedTasks > 4 & numberAcceptedTasks <= 10 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // 25% to 50%
replace treatment_a_serv12_int = 4 if(numberAcceptedTasks > 10 & numberAcceptedTasks <= 20 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // 50% to 75%
replace treatment_a_serv12_int = 5 if(numberAcceptedTasks > 20 & numberAcceptedTasks <= 43 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // 75% to 90%
replace treatment_a_serv12_int = 6 if(numberAcceptedTasks > 43 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & (service_1 == 1 | service_2 == 1) & (numberTransport > 0 | numberCommunityResponse > 0))) // highest 10%
replace treatment_a_serv12_int = 0 if((allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) & (service_1 == 1 | service_2 == 1)) // Include timed-out tasks (if any)

generate treatment_a_service3_int = .
replace treatment_a_service3_int = 1 if(numberAcceptedTasks == 1 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // lowest 10%
replace treatment_a_service3_int = 2 if(numberAcceptedTasks >= 2 & numberAcceptedTasks <= 2 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // 10% to 25%
replace treatment_a_service3_int = 3 if(numberAcceptedTasks > 2 & numberAcceptedTasks <= 7 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // 25% to 50%
replace treatment_a_service3_int = 4 if(numberAcceptedTasks > 7 & numberAcceptedTasks <= 16 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // 50% to 75%
replace treatment_a_service3_int = 5 if(numberAcceptedTasks > 16 & numberAcceptedTasks <= 34 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // 75% to 90%
replace treatment_a_service3_int = 6 if(numberAcceptedTasks > 34 & (allocatedTask == 1 & acceptedTask == 1 & completedTask == 1 & service_3 == 1 & numberCheckInAndChat > 0)) // highest 10%
replace treatment_a_service3_int = 0 if((allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0)) & service_3 == 1) // Include timed-out tasks (if any)

// Controls
///////////

// Post Codes

generate postcode_admin_aggregated2 = substr(pafpostcode, 1, 2)
tabulate postcode_admin_aggregated2, generate(postcode_admin_aggregated2_)


//////////////
// Analysis //
//////////////

// Preparation
//////////////

// Balancedness

generate balancedness = 1 if(!missing(satisfaction_life, worthwhileness_life, belongingness, connectedness))

// Outcomes (Shortened)

generate sl = satisfaction_life
generate wl = worthwhileness_life
generate b = belongingness
generate c = connectedness

// Covariates

global x1 "i.age2 i.gender i.ethnicity2 i.religion i.condition i.isolating i.volunteered_before i.volunteered_elsewhere i.volunteered_elsewhere_where"
global x2 "motivations2_*"
global x3 ""
global x4 "fidelity"
global y1 ""
global y2 "i.date i.week_day "
global z "newAdmissions cumAdmissions hospitalCases covidOccupiedMVBeds newCasesBySpecimenDate cumCasesBySpecimenDate newDeaths28DaysByDeathDate cumDeaths28DaysByDeathDate"

